package com.snail.billing.data;

import android.text.TextUtils;

import com.snail.billing.util.AesUtil;
import com.snail.billing.util.BillingConfiguration;

public class AccountManager {

	private static final int SAVE_MAX = Integer.MAX_VALUE;

	private static Account currentAccount;
	private static AccountList accountList;

	public static void initAccountManager() {
		BillingConfiguration billingConfiguration = new BillingConfiguration();
		// 同步数据
		AccountFile accountFile = new AccountFile();
		String account = billingConfiguration.getAccount();
		if (TextUtils.isEmpty(account)) {
			account = accountFile.getAccount();
			if (!TextUtils.isEmpty(account)) {
				billingConfiguration.setAccount(account);
			}
		} else {
			accountFile.setAccount(account);
		}
		// 初始化账号列表
		accountList = new AccountList();
		try {
			if (!TextUtils.isEmpty(account)) {
				accountList = new AccountList(AesUtil.decrypt(account));
			}
		} catch (Exception e) {
		}
		// 初始化当前账号
		try {
			String currentAccount = billingConfiguration.getCurrentAccount();
			if (!TextUtils.isEmpty(currentAccount)) {
				AccountManager.currentAccount = new Account(AesUtil.decrypt(currentAccount));
			}
		} catch (Exception e) {
		}
	}

	public static Account getCurrentAccount() {
		return currentAccount;
	}

	public static void setCurrentAccount(Account account) {
		currentAccount = account;
		String accountJsonString = AesUtil.encrypt(account.toString());
		BillingConfiguration billingConfiguration = new BillingConfiguration();
		billingConfiguration.setCurrentAccount(accountJsonString);
	}

	public static void clearCurrentAccount() {
		currentAccount = null;
		BillingConfiguration billingConfiguration = new BillingConfiguration();
		billingConfiguration.setCurrentAccount("");
	}

	public static AccountList getAccountList() {
		return accountList;
	}

	public static void setAccount(Account account) {
		AccountList oldAccountList = getAccountList();
		AccountList newAccountList = new AccountList();
		// 删除相同的账号
		Account tempAccount = null;
		for (Account oldAccount : oldAccountList.getAll()) {
			if (!oldAccount.getAccount().equalsIgnoreCase(account.getAccount())) {
				newAccountList.add(oldAccount);
			} else {
				tempAccount = oldAccount;
			}
		}
		// 设置账号类型
		if (account.getType().length() == 0) {
			if (tempAccount != null) {
				account.setType(tempAccount.getType());
			} else {
				// 新账号
				account.setType(Account.TYPE_COMMON);
			}
		}
		setCurrentAccount(account);
		// 加入账号
		newAccountList.addFirst(account);
		// 删除大于最大值的不是随机进入账号
		if (newAccountList.length() >= SAVE_MAX) {
			for (int i = newAccountList.length() - 1; i >= 0; i--) {
				Account newAccount = newAccountList.getAll().get(i);
				if (newAccount.getType().equals(Account.TYPE_RANDOM)) {
					continue;
				} else {
					newAccountList.remove(newAccount);
					break;
				}
			}
		}
		// 保存
		saveAll(newAccountList);
	}
	
	public static Account getRandomAccount() {
		AccountList oldAccountList = getAccountList();
		for (Account oldAccount : oldAccountList.getAll()) {
			if (oldAccount.getType().equals(Account.TYPE_RANDOM)) {
				return oldAccount;
			}
		}
		return null;
	}

	public static void setRandomAccount(Account account) {
		// 设置账号类型
		account.setType(Account.TYPE_RANDOM);
		setCurrentAccount(account);
		AccountList oldAccountList = getAccountList();
		AccountList newAccountList = new AccountList();
		// 删除随机的账号
		for (Account oldAccount : oldAccountList.getAll()) {
			if (!oldAccount.getType().equals(Account.TYPE_RANDOM)) {
				newAccountList.add(oldAccount);
			}
		}
		// 加入账号
		newAccountList.addFirst(account);
		// 删除大于最大值的不是随机进入账号
		if (newAccountList.length() >= SAVE_MAX) {
			for (int i = newAccountList.length() - 1; i >= 0; i--) {
				Account newAccount = newAccountList.getAll().get(i);
				if (newAccount.getType().equals(Account.TYPE_RANDOM)) {
					continue;
				} else {
					newAccountList.remove(newAccount);
					break;
				}
			}
		}
		// 保存
		saveAll(newAccountList);
	}

	public static void setBindAccount(Account account) {
		// 设置账号类型
		account.setType(Account.TYPE_COMMON);
		setCurrentAccount(account);
		//
		AccountList accountList = getAccountList();
		accountList.removeFirst();
		accountList.addFirst(account);
		// 保存
		saveAll(accountList);
	}

	public static void removeAccount(Account account) {
		AccountList oldAccountList = getAccountList();
		AccountList newAccountList = new AccountList();
		// 删除账号
		for (Account oldAccount : oldAccountList.getAll()) {
			if (!oldAccount.getAccount().equalsIgnoreCase(account.getAccount())) {
				newAccountList.add(oldAccount);
			}
		}
		// 保存
		saveAll(newAccountList);
	}

	private static void saveAll(AccountList accountList) {
		BillingConfiguration billingConfiguration = new BillingConfiguration();
		String account = AesUtil.encrypt(accountList.toString());
		billingConfiguration.setAccount(account);
		AccountFile accountFile = new AccountFile();
		accountFile.setAccount(account);
	}
}
